package ar.republicagroup.reportes.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class ConnectionDB {
    
    
     private static ConnectionDB instance;
     private static Logger log = Logger.getLogger(ConnectionDB.class);
     public static final String DB_CONFIG_FILE_ABSOLUTE_PATH = "C:\\Program Files\\Republica Group\\reports\\config\\database.properties";
     
        private ConnectionDB(){		

        }	
     
        public static ConnectionDB getInstance(){
		if(instance == null){
                    instance = new ConnectionDB();
                }
		return instance;
	}
        
        public Connection getConnection(){		
		Connection cn = null;		
		Properties prop = new Properties();
                
                try{
                InputStream inputStreamConfig = new FileInputStream(DB_CONFIG_FILE_ABSOLUTE_PATH);
		prop.load(inputStreamConfig);
                Class.forName(prop.getProperty("driver"));
		cn = DriverManager.getConnection(prop.getProperty("url"), 
                                                 prop.getProperty("user").trim(), 
                                                 prop.getProperty("pass").trim());	
                }catch(Exception ex){
                   log.error("Error al obtener conexión en getConnection()");
                   log.error(ex.getMessage(), ex);
                }
                
		return cn;
	}
	
	public void closeConnection(Connection cn) throws SQLException {
		try {
			if(cn != null){ 
                            cn.close();
                        }
		}catch(SQLException ex){
                    throw new SQLException("Error en closeConnection()", ex);
		}			
	}
        
        
	public void closeStatement(Statement stm) throws SQLException {
		try {
			if(stm != null){ 
				stm.close();
                        }        
                                
		}catch(SQLException ex){
                    throw new SQLException("Error en closeStatement()", ex);
		}			
	}
        
        
	public void closeResultSet(ResultSet rs) throws SQLException {
		try {
			if(rs != null){ 
				rs.close();
                        }        
		}catch(SQLException ex){
                    throw new SQLException("Error en closeStatement()", ex);
		}			
	}


    
}
